<template>
  <div class="mobile-container">
    <div class="app-content">
      <!-- 顶部栏 -->
      <div class="header">
        <div class="header-top">
          <div class="back-icon" @click="handleBack">
            <Icon icon="mdi:arrow-left"/>
          </div>
          <div class="header-title">数量</div>
          <div class="scan-icon" @click="handleScan">
            <Icon icon="mdi:line-scan"/>
          </div>
        </div>
      </div>

      <!-- 数量卡片 -->
      <div class="balance-card">
        <div class="balance-item" @click="showConvertDialog">
          <div class="balance-label">WP广告数量</div>
          <div class="balance-value">0</div>
        </div>
        <div class="balance-divider"></div>
        <div class="balance-item" @click="handleFrozenClick">
          <div class="balance-label">冻结中</div>
          <div class="balance-value">0</div>
        </div>
      </div>

      <!-- 选项卡 -->
      <div class="tab-container">
        <div
          class="tab-scroll"
          ref="tabScroll"
          @scroll="checkScroll"
          @wheel.prevent="handleWheel"
        >
          <div
            v-for="(tab, index) in tabs"
            :key="index"
            :class="['tab-item', { active: activeTab === tab }]"
            @click="switchTab(tab)"
          >
            {{ tab }}
          </div>
        </div>
      </div>

      <!-- 列表内容 -->
      <div class="content-list">
        <div class="empty-state" v-if="listData.length === 0">
          <Icon icon="mdi:file-document-outline" class="empty-icon"/>
          <p class="empty-text">暂无广告记录</p>
        </div>
        <div v-else class="list-items">
          <!-- 列表项内容 -->
          <div v-for="(item, index) in listData" :key="index" class="list-item">
            {{ item }}
          </div>
        </div>
      </div>

      <!-- 转换弹窗 -->
      <div v-if="convertDialogVisible" class="custom-dialog-mask">
        <div class="custom-dialog">
          <div class="dialog-header">
            <h3>转换到凤凰潮WP</h3>
            <span class="close-btn" @click="convertDialogVisible = false">×</span>
          </div>
          <div class="dialog-body">
            <div class="input-group">
              <label class="input-label">转换数量</label>
              <input
                v-model="convertAmount"
                placeholder="请输入转换数量"
                type="number"
                class="custom-input"
              >
            </div>

            <div class="input-group">
              <label class="input-label">转换密码</label>
              <div class="password-input-wrapper">
                <input
                  v-model="convertPassword"
                  :type="showPassword ? 'text' : 'password'"
                  placeholder="请输入转换密码"
                  class="custom-input"
                >
                <div class="password-toggle" @click="showPassword = !showPassword">
                  <Icon :icon="showPassword ? 'mdi:eye-off' : 'mdi:eye'"/>
                </div>
              </div>
            </div>

            <button class="convert-confirm-btn" @click="handleConvertConfirm">
              转换到凤凰潮WP
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Icon } from '@iconify/vue2'

export default {
  name: 'WPPQuantity',
  components: {
    Icon
  },
  data() {
    return {
      activeTab: '全部',
      tabs: ['全部', '转让', '需求', 'WP兑入', '返还', '转凤凰潮', '工分转入', '商家兑入'],
      showLeftScroll: false,
      showRightScroll: true,
      listData: [],
      convertDialogVisible: false,
      convertAmount: '',
      convertPassword: '',
      showPassword: false
    }
  },
  methods: {
    // 返回上一页
    handleBack() {
      this.$router.go(-1)
    },

    // 扫码
    handleScan() {
      alert('打开扫码')
    },

    // 切换选项卡
    switchTab(tab) {
      this.activeTab = tab
      this.loadListData(tab)
    },

    // 冻结中点击
    handleFrozenClick() {
      alert('打开冻结中')
    },

    // 显示转换弹窗
    showConvertDialog() {
      this.convertDialogVisible = true
    },

    // 确认转换
    handleConvertConfirm() {
      if (!this.convertAmount) {
        alert('请输入转换数量')
        return
      }
      if (!this.convertPassword) {
        alert('请输入转换密码')
        return
      }

      console.log('转换数量:', this.convertAmount)
      console.log('转换密码:', this.convertPassword)

      alert('转换成功')
      this.convertDialogVisible = false
      this.convertAmount = ''
      this.convertPassword = ''
    },

    // 加载列表数据
    loadListData(tab) {
      this.listData = []
    },

    // 检查滚动状态
    checkScroll() {
      const scrollEl = this.$refs.tabScroll
      if (scrollEl) {
        this.showLeftScroll = scrollEl.scrollLeft > 0
        this.showRightScroll = scrollEl.scrollLeft < (scrollEl.scrollWidth - scrollEl.clientWidth)
      }
    },

    // 鼠标滚轮滚动
    handleWheel(event) {
      const scrollEl = this.$refs.tabScroll
      if (scrollEl) {
        scrollEl.scrollLeft += event.deltaY
      }
    },

    setupTouchScroll() {
      const scrollEl = this.$refs.tabScroll
      if (!scrollEl) return

      let isDragging = false
      let startX
      let scrollLeft

      // 鼠标事件
      scrollEl.addEventListener('mousedown', (e) => {
        isDragging = true
        startX = e.pageX - scrollEl.offsetLeft
        scrollLeft = scrollEl.scrollLeft
        scrollEl.style.cursor = 'grabbing'
        scrollEl.style.userSelect = 'none'
      })

      scrollEl.addEventListener('mouseleave', () => {
        isDragging = false
        scrollEl.style.cursor = 'grab'
      })

      scrollEl.addEventListener('mouseup', () => {
        isDragging = false
        scrollEl.style.cursor = 'grab'
        scrollEl.style.userSelect = 'auto'
      })

      scrollEl.addEventListener('mousemove', (e) => {
        if (!isDragging) return
        e.preventDefault()
        const x = e.pageX - scrollEl.offsetLeft
        const walk = (x - startX) * 2
        scrollEl.scrollLeft = scrollLeft - walk
      })

      // 触摸事件
      scrollEl.addEventListener('touchstart', (e) => {
        isDragging = true
        startX = e.touches[0].pageX - scrollEl.offsetLeft
        scrollLeft = scrollEl.scrollLeft
      })

      scrollEl.addEventListener('touchmove', (e) => {
        if (!isDragging) return
        const x = e.touches[0].pageX - scrollEl.offsetLeft
        const walk = (x - startX) * 2
        scrollEl.scrollLeft = scrollLeft - walk
      })

      scrollEl.addEventListener('touchend', () => {
        isDragging = false
      })
    }
  },
  mounted() {
    this.loadListData(this.activeTab)
    this.$nextTick(() => {
      this.checkScroll()
      this.setupTouchScroll()
    })
  }
}
</script>

<style scoped>
.mobile-container {
  width: 100%;
  min-height: 100vh;
  background: #f5f5f5;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
  padding: 20px;
}

.app-content {
  width: 375px;
  min-height: 667px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  position: relative;
  display: flex;
  flex-direction: column;
}

/* 顶部栏样式 */
.header {
  padding: 15px 20px;
  background: white;
  border-bottom: 1px solid #f0f0f0;
}

.header-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.back-icon, .scan-icon {
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  color: #333;
}

.back-icon:hover, .scan-icon:hover {
  background: #f5f5f5;
}

.header-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

/* 数量卡片样式 */
.balance-card {
  background: white;
  margin: 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  display: flex;
  overflow: hidden;
}

.balance-item {
  flex: 1;
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.balance-item:hover {
  background: #f8f9fa;
}

.balance-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
}

.balance-value {
  font-size: 24px;
  font-weight: 700;
  color: #333;
}

.balance-divider {
  width: 1px;
  background: #f0f0f0;
}

/* 选项卡样式 */
.tab-container {
  position: relative;
  background: white;
  padding: 0 40px;
  margin-bottom: 10px;
}

.tab-scroll {
  display: flex;
  overflow-x: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
  padding: 12px 0;
  cursor: grab;
  scroll-behavior: smooth;
}

.tab-scroll:active {
  cursor: grabbing;
}

.tab-scroll::-webkit-scrollbar {
  display: none;
}

.tab-item {
  flex-shrink: 0;
  padding: 8px 16px;
  font-size: 14px;
  color: #666;
  cursor: pointer;
  border-radius: 16px;
  margin-right: 8px;
  transition: all 0.3s ease;
  white-space: nowrap;
  border: 1px solid transparent;
}

.tab-item:hover {
  background: #f5f5f5;
}

.tab-item.active {
  background: #1890ff;
  color: white;
  border-color: #1890ff;
}

.scroll-indicator {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 28px;
  background: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  cursor: pointer;
  z-index: 2;
  font-size: 18px;
  color: #666;
  transition: all 0.3s ease;
  border: none;
}

.scroll-indicator:hover {
  background: #f5f5f5;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.scroll-indicator.left {
  left: 8px;
}

.scroll-indicator.right {
  right: 8px;
}

/* 列表内容样式 */
.content-list {
  flex: 1;
  background: white;
  margin: 0 20px 20px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.empty-state {
  padding: 60px 20px;
  text-align: center;
  color: #999;
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.empty-text {
  font-size: 16px;
  margin: 0;
}

.list-items {
  width: 100%;
}

.list-item {
  padding: 16px 20px;
  border-bottom: 1px solid #f0f0f0;
  font-size: 14px;
  color: #333;
}

.list-item:last-child {
  border-bottom: none;
}

/* 弹窗样式 */
.custom-dialog-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  border-radius: 20px;
}

.custom-dialog {
  width: 280px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}

.dialog-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #f0f0f0;
  background: #fafafa;
}

.dialog-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
}

.close-btn {
  font-size: 20px;
  color: #999;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.close-btn:hover {
  color: #666;
}

.dialog-body {
  padding: 20px;
}

.input-group {
  margin-bottom: 20px;
}

.input-label {
  display: block;
  margin-bottom: 8px;
  font-size: 14px;
  font-weight: 500;
  color: black;
}

.custom-input {
  width: 100%;
  height: 44px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  outline: none;
  transition: all 0.3s ease;
  background: white;
}

.custom-input:focus {
  border-color: #1890ff;
  background: white;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.1);
}

.custom-input::placeholder {
  color: #999;
}

.password-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.password-input-wrapper .custom-input {
  padding-right: 40px;
}

.password-input-wrapper .password-toggle {
  position: absolute;
  right: 12px;
  color: #666;
  cursor: pointer;
  font-size: 18px;
  padding: 4px;
}

.convert-confirm-btn {
  width: 100%;
  height: 44px;
  background: #1890ff;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  margin-top: 8px;
  transition: all 0.3s ease;
}

.convert-confirm-btn:hover {
  background: #40a9ff;
}

.convert-confirm-btn:active {
  background: #096dd9;
  transform: translateY(1px);
}

/* 响应式设计 */
@media (max-width: 375px) {
  .mobile-container {
    padding: 0;
  }

  .app-content {
    width: 100%;
    min-height: 100vh;
    border-radius: 0;
  }

  .tab-container {
    padding: 0 30px;
  }

  .scroll-indicator {
    width: 24px;
    height: 24px;
    font-size: 16px;
  }
}
</style>
